package com.shuosen.gmall.payment.mq;

import com.alibaba.dubbo.config.annotation.Reference;
import com.shuosen.gmall.bean.PaymentInfo;
import com.shuosen.gmall.service.PaymentService;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.MapMessage;

@Component
public class PaymentConsumer {

    @Reference
    PaymentService paymentService ;

    @JmsListener(destination = "PAYMENT_RESULT_CHECK_QUEUE",containerFactory = "jmsQueueListener")
    public void consumerPaymentResultCheck(MapMessage mapMessage) throws JMSException {
        // 得到数据
        String outTradeNo = mapMessage.getString("outTradeNo");
        int delaySec = mapMessage.getInt("delaySec");
        int checkCount = mapMessage.getInt("checkCount");
        //如果发现PAYMENT_RESULT_CHECK_QUEUE消息则执行  [payment.gmall.com]
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setOutTradeNo(outTradeNo);
        paymentService.getPaymentInfo(paymentInfo);
        boolean flag = paymentService.checkPayment(paymentInfo);
        if(!flag&&checkCount>0){
            System.out.println("再次发送");
            paymentService.sendDelayPaymentResult(outTradeNo,delaySec,checkCount-1);
        }

    }
}
